//
//  CircleTableViewController.swift
//  Hunter
//
//  Created by oher on 15/6/24.
//  Copyright © 2015年 刘🐑. All rights reserved.
//

import UIKit

let CircleTableViewControllerCell = "CircleTableViewControllerCell"

class CircleTableViewController: UITableViewController, ContractScanViewControllerDelegate {

    var Contracts: NSArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        setupTableView()
        
    }
    
    override func loadView() {
        tableView = UITableView(frame: CGRectZero, style: UITableViewStyle.Grouped)
        
    }
    
    func loadData(){
        let path = NSBundle.mainBundle().pathForResource("Circle.plist", ofType: nil)
        let Contracts = NSArray(contentsOfFile: path!)
        self.Contracts = Contracts
        
    }
    
    func setupTableView(){
        addNavigationBarButton()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CircleTableViewControllerCell)
        
    }
    
    func addNavigationBarButton(){
        // add button
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "add_friend"), style: UIBarButtonItemStyle.Done, target: self, action: "showScan_AddFromContact")
        
        //search bar
        
        
        
    }

    //导航栏
    func showScan_AddFromContact(){
        
        //UIPresentationController
        let contractScanFormSB = UIStoryboard(name: "ContractScan", bundle: nil).instantiateViewControllerWithIdentifier("ContractScanID") as! ContractScanViewController
        contractScanFormSB.transitioningDelegate = self
        contractScanFormSB.modalPresentationStyle = UIModalPresentationStyle.Custom
        contractScanFormSB.delegate = self
        presentViewController(contractScanFormSB, animated: true, completion: nil)
        
    
    }
    
    func forwardToAnoherController(CSController: ContractScanViewController, index: Int){
        let viewController = UIViewController()
        viewController.hidesBottomBarWhenPushed = true

        if index == 0{
            //联系人列表
            viewController.view.backgroundColor = UIColor.orangeColor()
            navigationController?.pushViewController(viewController, animated: true)
        }else{
            //扫一扫
            ///  显示二维码界面
            let sb = UIStoryboard(name: "QRCode", bundle: nil)
            presentViewController(sb.instantiateInitialViewController()!, animated: true, completion: nil)
            //navigationController?.pushViewController(sb.instantiateInitialViewController()!, animated: true)
        }
        
    }
    
    
    //代理方法

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CircleTableViewControllerCell, forIndexPath: indexPath)
        let dict = Contracts![indexPath.row]
        cell.imageView?.image = UIImage(named: dict["icon"] as! String)
        cell.textLabel?.text = dict["title"] as? String
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        //自定义cell
        //注册可重用单元格
        //设置数据对象
        //返回cell
        return cell
    }
    
    var isPresent = false

}

//转场代理
extension CircleTableViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning{
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{
        
        return PresetntationController(presentedViewController: presented, presentingViewController: presenting)
    }
    
    //
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        return self
    }
    
    //
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
        return self
    }

    

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        
        // 1. 获取目标视图
        let viewKey = isPresent ? UITransitionContextToViewKey : UITransitionContextFromViewKey
        let targetView = transitionContext.viewForKey(viewKey)
        
        if targetView == nil {
            return
        }
        
        if isPresent {
            // 2. 添加 toView
            transitionContext.containerView()?.addSubview(targetView!)
            targetView!.alpha = 0
            
            // 3. 动画
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                targetView!.alpha = 1
                }, completion: { (_) -> Void in
                    // *** 告诉动画完成
                    transitionContext.completeTransition(true)
            })
        } else {
            // 2. 动画
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                targetView!.alpha = 0
                }, completion: { (_) -> Void in
                    targetView?.removeFromSuperview()
                    transitionContext.completeTransition(true)
            })
        }
        
    }
    


}
